package com.itboye.textileexpres.myset;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import com.example.textileexpres.widget.ClipImageLayout;
import com.example.textileexpres.widget.ImageTools;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ClipActivity extends BaseActivity {
	private ClipImageLayout mClipImageLayout;
	private String path;
	private ProgressDialog loadingDialog;

//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);// ȥ��������
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_clipimage);
//
//	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_clipimage;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		// 这步必须要加
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		loadingDialog = new ProgressDialog(this);
		loadingDialog.setTitle("正在加载中");
		path = getIntent().getStringExtra("path");
		if (TextUtils.isEmpty(path) || !(new File(path).exists())) {
			Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
			return;
		}
		Bitmap bitmap = ImageTools.convertToBitmap(path, 500, 500);
		if (bitmap == null) {
			Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
			return;
		}
		mClipImageLayout = (ClipImageLayout) findViewById(R.id.id_clipImageLayout);
		mClipImageLayout.setBitmap(bitmap);
		((Button) findViewById(R.id.id_action_clip))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						loadingDialog.show();
						new Thread(new Runnable() {
							@Override
							public void run() {
								Bitmap bitmap = mClipImageLayout.clip();

								String path = Environment
										.getExternalStorageDirectory()
										+ "/ClipHeadPhoto/cache/"
										+ System.currentTimeMillis() + ".png";
								ImageTools.savePhotoToSDCard(bitmap, path);
								loadingDialog.dismiss();
								Intent intent = new Intent();
								intent.putExtra("path", path);
								setResult(RESULT_OK, intent);
								finish();
							}
						}).start();
					}
				});
	}

}
